package io.bba.boot.app.controller;

import io.bba.boot.auth.annotation.Permission;
import io.bba.boot.common.enums.SysLogType;
import io.bba.boot.framework.annotation.Log;
import io.bba.boot.framework.page.Paging;
import io.bba.boot.framework.response.ApiResult;
import io.bba.boot.app.dto.CrawlerDataDto;
import io.bba.boot.app.query.CrawlerDataQuery;
import io.bba.boot.app.service.CrawlerDataService;
import io.bba.boot.app.vo.CrawlerDataVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 采集数据 控制器
 *
 * @author zs
 * @since 2024-03-19
 */
@Slf4j
@RestController
@Tag(name = "采集数据")
@RequestMapping("/admin/crawlerData")
public class CrawlerDataController {

    @Autowired
    private CrawlerDataService crawlerDataService;

    /**
     * 添加采集数据
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.ADD)
    @Operation(summary = "添加采集数据")
    @PostMapping("/addCrawlerData")
    @Permission("crawler:data:add")
    public ApiResult addCrawlerData(@Valid @RequestBody CrawlerDataDto dto) {
        log.info("添加采集数据：{}", dto);
        boolean flag = crawlerDataService.addCrawlerData(dto);
        return ApiResult.result(flag);
    }

    /**
     * 修改采集数据
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.UPDATE)
    @Operation(summary = "修改采集数据")
    @PostMapping("/updateCrawlerData")
    @Permission("crawler:data:update")
    public ApiResult updateCrawlerData(@Valid @RequestBody CrawlerDataDto dto) {
        log.info("修改采集数据：{}", dto);
        boolean flag = crawlerDataService.updateCrawlerData(dto);
        return ApiResult.result(flag);
    }

    /**
     * 删除采集数据
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.DELETE)
    @Operation(summary = "删除采集数据")
    @PostMapping("/deleteCrawlerData/{id}")
    @Permission("crawler:data:delete")
    public ApiResult deleteCrawlerData(@PathVariable Long id) {
        log.info("删除采集数据：{}", id);
        boolean flag = crawlerDataService.deleteCrawlerData(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取采集数据详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取采集数据详情")
    @PostMapping("/getCrawlerData/{id}")
    @Permission("crawler:data:info")
    public ApiResult<CrawlerDataVo> getCrawlerData(@PathVariable Long id) {
        log.info("获取采集数据详情：{}", id);
        CrawlerDataVo crawlerDataVo = crawlerDataService.getCrawlerDataById(id);
        return ApiResult.success(crawlerDataVo);
    }

    /**
     * 获取采集数据分页列表
     *
     * @param query
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取采集数据分页列表")
    @PostMapping("/getCrawlerDataPage")
    @Permission("crawler:data:page")
    public ApiResult<CrawlerDataVo> getCrawlerDataPage(@Valid @RequestBody CrawlerDataQuery query) {
        log.info("获取采集数据分页列表：{}", query);
        Paging<CrawlerDataVo> paging = crawlerDataService.getCrawlerDataPage(query);
        return ApiResult.success(paging);
    }

}
